import type { Respond, User } from "@/types";
import request from "@/utils/request";
import md5 from "js-md5";
export function token(code: string) {
  return request<
    null,
    Respond & {
      sessionJson: { openid: string; session_key: string; token: string };
    }
  >({
    url: "/token",
    header: {
      Authorization: "",
    },
    data: {
      code,
    },
  });
}

export function login(js_code: string) {
  return request<
    null,
    Respond & {
      sessionJson: { openid: string; session_key: string; token: string };
    }
  >({
    url: "wechat/Account/wechat_login",
    header: {
      Authorization: "",
    },
    data: {
      js_code,
    },
  });
}

export function phoneNumber(code: string) {
  return request<null, Respond>({
    url: "wechat/Account/getuserphonenumber",
    header: {
      Authorization: "",
    },
    data: {
      code,
    },
  });
}

export function accountLogin(data: Record<string, any>) {
  return request<null, Respond>({
    url: "/admin-api/system/auth/login",
    header: {
      Authorization: "",
    },
    data: {
      username: data.username,
      password: data.password,
      tenantId: data.tenantId,
    },
  });
}

// 获取用户详细信息
export function getInfo() {
  return request<null, Respond & { user: User }>({
    url: "/getInfo",
    method: "GET",
  });
}

export function updateUser(params: Record<string, any>) {
  return request({
    url: "/system/user/profile/updateUser",
    data: {
      params,
    },
  });
}

// 获取验证码
export async function getCodeImg(): Promise<{
  captchaEnabled?: boolean;
  uuid: string;
  img: string;
}> {
  await uni.removeStorage({
    key: "cookies",
  });
  return request({
    method: "POST",
    url: "/Base/verification_code",
    responseType: "arraybuffer",
    data: {},
  });
}
